package rdgl.compiler;

/**
 * 
 * @author Navid
 *
 */
public class RandomData {

	private DataType type;
	private Range range;
	private String value;
	
	/**
	 * 
	 *
	 */
	public RandomData() {
		this.range = new Range();
		this.value = new String();
	}
	
	/**
	 * 
	 * @param type
	 */
	public void setType(DataType type) {
		this.type = type;
	}
	
	/**
	 * 
	 * @param range
	 */
	public void setRange(Range range) {
		this.range = range;
	}
	
	/**
	 * Appends the current string to value.
	 * @param val
	 */
	public void append(String val) {
		this.value += val;
	}
	
	/**
	 * Returns the current value of the expression.
	 * @return
	 */
	public String getValue() {
		return this.value;
	}
	
	/**
	 * Override the default implementation of toString() for this class.
	 */
	public String toString() {
		return "{type: " + this.type + ", value: " + this.value + "}";
	}
	
	/**
	 * Outputs the current value of the expression after it has been
	 * randomly generated without a newline.
	 *
	 */
	public void print() {
		System.out.print(this.value);
	}

	/**
	 * Outputs the current value of the expression after it has been
	 * randomly generated with a newline.
	 *
	 */
	public void println() {
		System.out.println(this.value);
	}
	
	/**
	 * 
	 *
	 */
	public String calculate() {
		if(this.range.getSize() == 0) {
			this.value = calculateLength(1);
			return this.value;
		}
		
		int index = Integer.parseInt(Engine.getRandomNumber(0, this.range.getSize() - 1));
		String contents = this.range.get(index);
		
		if (this.type == DataType.Enum) {
			this.value = contents;  
		} else if (this.range.getType() == Range.Type.Length) {
			this.value = calculateLength(Integer.parseInt(contents));
		} else if (this.range.getType() == Range.Type.Range) {
			this.value = contents;
		}
		return this.value;
	}
	
	/**
	 * 
	 * @param length
	 * @return
	 */
	private String calculateLength(int length) {
		if(this.type == DataType.Number) {
			return Engine.getRandomNumber(length);
		} else if (this.type == DataType.Letter) {
			return Engine.getRandomCharacter(length);
		} else if (this.type == DataType.AlphaNumeric) {
			return Engine.getRandomAlphaNumeric(length);
		} else if (this.type == DataType.Symbol) {
			return Engine.getRandomSymbol(length);
		} else if (this.type == DataType.Any) {
			return Engine.getRandomString(length);
		} else {
			assert false;
			return null;
		}
	}
}
